// 业务相关的函数
import { log, info, warn, error } from "@/util/tio"

import High from "@/comps/High";
import OffAutoComplete from "@/comps/OffAutoComplete";
import { gl_opt, tiourl, get, post } from '@/util/http';

import { Button, Form, Input, Link, Space, Typography } from '@arco-design/web-react';
import md5 from 'md5';
import React, { useContext, useState } from "react";
import { selectRoot, upState, update } from "@/store/rootSlice";
import { useDispatch, useSelector } from "react-redux";


/**
 * 获取当前用户
 */
export async function curr({ dispatch }, func?: Function) {
    let { data: resp } = await get(`/user/curr`);
    const { ok, data, code, msg } = resp;
    if (ok) {
        update({ curr: data }, dispatch)
    }
    if (func) {
        func({ ok, data, code, msg });
    }
    return { ok, data, code, msg };
}

/**
 * 登录
 * @param param1 
 * @param param2 
 * @param func 
 */
export async function login({ loginname, pwd, authcode }, { dispatch }, func?: Function) {
    let params = { loginname }
    if (pwd) {
        let pd5: String = md5("${" + loginname + "}" + pwd)
        params['pd5'] = pd5;
    }
    if (authcode) {
        params['authcode'] = authcode;
    }

    const { config, data: resp, headers, request, statusText, status } = await post(`/login`, params);
    const { ok, data, code, msg } = resp;
    if (ok) {
        const { ok, data, code, msg } = await curr({ dispatch });
        if (func) {
            func({ ok, data, code, msg });
        }
    }
}

/**
 * 
 * @param param1 
 * @param func 
 */
export async function logout({ dispatch }, func?: Function) {
    const { data: resp } = await post(`/logout`);
    const { ok, data, code, msg } = resp;
    if (ok) {
        update({ curr: null }, dispatch)
        if (func) {
            func({ ok, data, code, msg });
        }
    }
}